package com.qc.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.print.attribute.standard.Copies;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qc.common.Session;
import com.qc.entity.BrowseEntity;
import com.qc.entity.BusinessReceive;
import com.qc.entity.Coupon;
import com.qc.service.BusinessReceiveService;
import com.qc.service.CouponService;
import com.qc.service.UserDateService;

import net.sf.json.JSONObject;


/**
 * 我的资料
 * @author houzhong
 *
 */
@Controller
public class UserDateController extends BaseController{

	@Resource(name="userDateService")
	private UserDateService userDateService;
	
	@Resource(name="couponService")
	private CouponService couponService;
	
	@Resource(name="businessReceiveService")
	private BusinessReceiveService businessReceiveService;
	
	/**
	 * 我的  最近来访
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileBrowseGetAll", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileBrowseGetAll(HttpServletRequest request, HttpServletResponse response) {

		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		List<BrowseEntity> list = userDateService.listAllBrowse(0, userId, 1,10);
		
		return returnSuccessResult(list);
	}
	
	/**
	 * 我的  添加最近来访人
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileBrowseSave", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileBrowseSave(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		BrowseEntity browseEntity = (BrowseEntity)JSONObject.toBean(JSONObject.fromObject(p1), BrowseEntity.class);
		
		BrowseEntity browseEntity2 = userDateService.getBrowseId(userId, browseEntity.getReceiveId());
		if (browseEntity2.getUserId() != 0) {
			
			browseEntity2.setUserId(userId);
			browseEntity2.setReceiveId(browseEntity.getReceiveId());
			
			userDateService.updateBrowse(browseEntity2);
		}else {
			
			browseEntity.setUserId(userId);
			userDateService.saveBrowse(browseEntity);
		}
		
		//给被访问者发推送
//		JPushUtil.redDotPush(3, browseEntity2.getReceiveId(), "有人访问您的微主页,点击查看!");
		
		return returnSuccessResult("访问成功");
		
	}
	
	/**
	 * 我的 所有优惠券
	 */
	@RequestMapping(value = "/MobileGetMerchanListReceive", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileGetMerchanListReceive(HttpServletRequest request, HttpServletResponse response) {

		String p2 = request.getParameter("p2");
		
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		List<Coupon> list = couponService.listAllUserId(userId,"");
		
		return returnSuccessResult(list);
	}
	
	/**
	 * 我的  最近来访列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileBrowseGetAllAccess", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileBrowseGetAllAccess(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		BrowseEntity browseEntity = (BrowseEntity)JSONObject.toBean(JSONObject.fromObject(p1), BrowseEntity.class);
		List<BrowseEntity> list = null;
		//  1 来访者  2我访问别人
		if (1 == browseEntity.getType()) {
			
			list = userDateService.listAllBrowse(0, userId, browseEntity.getPageNo(),browseEntity.getPageSize());
		}else {
			list = userDateService.listAllBrowse(userId,0, browseEntity.getPageNo(),browseEntity.getPageSize());
		}
		
		for (BrowseEntity entity : list) {
			
			entity.setModifiedTime(entity.getModifiedTime().substring(0,19));
		}
		return returnSuccessResult(list);
	}
	
	/**
	 * 我的优惠券
	 */
	@RequestMapping(value = "/MobileReceiveCouponAll", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileReceiveCouponAll(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		BusinessReceive businessReceive = (BusinessReceive)JSONObject.toBean(JSONObject.fromObject(p1), BusinessReceive.class);
		
		List<BusinessReceive> list = businessReceiveService.listUserReceive(userId,0, businessReceive.getState(), businessReceive.getPageNo(), businessReceive.getPageSize());
		
		return returnSuccessResult(list);
	}
	
	
	/**
	 * 立即使用优惠券
	 */
	@RequestMapping(value = "/MobileUseCoupon", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileUseCoupon(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		BusinessReceive businessReceive = (BusinessReceive)JSONObject.toBean(JSONObject.fromObject(p1), BusinessReceive.class);
		
		businessReceive.setState(1);
		businessReceive.setUserId(userId);
		businessReceive.setbId(businessReceive.getbId());
		
		businessReceiveService.updateBusinessReceiveState(businessReceive);
		
		return returnSuccessResult("使用成功");
		
	}
	
	
	
}
